package qy.jalgotrade.utils.plotter;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import jep.JepException;
import jep.python.PyObject;
import qy.jalgotrade.bar.Bar.Frequency;
import qy.jalgotrade.utils.CommonUtils;

/**
 * @author c-geo
 *
 */
public class HistogramMarker extends Series<Double, Double> {

	/**
	 * 
	 * @param frequency frequency
	 */
	public HistogramMarker(Frequency frequency) {

		this(frequency, null);
	}

	/**
	 * @param frequency frequency
	 * @param label label
	 */
	public HistogramMarker(Frequency frequency, String label) {

		super(frequency, label);
	}

	/* (non-Javadoc)
	 * @see qy.jalgotrade.utils.plotter.Series#getMarker()
	 */
	@Override
	public String getMarker() {

		return null;
	}

	/* (non-Javadoc)
	 * @see qy.jalgotrade.utils.plotter.Series#needColor()
	 */
	@Override
	public boolean needColor() {

		return true;
	}

	/**
	 * 
	 * @param value value
	 * @return defaultColor
	 */
	public String getColorForValue(Double value, String defaultColor) {

		return defaultColor;
	}

	/*
	 * (non-Javadoc)
	 * @see qy.jalgotrade.utils.plotter.Series#plot(jep.python.PyObject, java.util.List, java.lang.String)
	 */
	@Override
	public void plot(PyObject mplSubplot, List<ZonedDateTime> dateTimes, String color) throws JepException {

		PyObject dateTimeIdx = StrategyPlotter.jepInterpreter().getValue("plotterDateTimeIdx", PyObject.class);

		PyObject values = CommonUtils.createPyList(StrategyPlotter.jepInterpreter(),
		        CommonUtils.genPyObjectName(getClass().getName(), "plot", getLabel() + "_" + "values"),
				true);
		PyObject colors = CommonUtils.createPyList(StrategyPlotter.jepInterpreter(),
		        CommonUtils.genPyObjectName(getClass().getName(), "plot", getLabel() + "_" + "colors"),
				true);
		for (ZonedDateTime dateTime : dateTimes) {

			Double value = getValue(dateTime);
			/*if (value != null && !Double.isNaN(value)) {
				CommonUtils.pyInvoke(values, "append", value);
				CommonUtils.pyInvoke(colors, "append", getColorForValue(value, color));
			}*/
			CommonUtils.pyInvoke(values, "append", value);
			CommonUtils.pyInvoke(colors, "append", value != null && !Double.isNaN(value) ? getColorForValue(value, color) : "gray");

		}
		// mplSubplot.bar(validDateTimes, values, color=colors, label=self.getLabel())
		Object[] args = new Object[] { dateTimeIdx, values };
		// @formatter:off
		Map<String, Object> kwargs = MapUtils.putAll(new HashMap<String, Object>(), new Object[][] {
				{ "color", colors },
				{ "label", getLabel() }
			});
		// @formatter:on
		CommonUtils.pyInvoke(mplSubplot, "bar", args, kwargs);
	}
}
